package cn.dgut.training.vo;

import cn.dgut.training.e.Status;
import lombok.Data;

import java.io.Serializable;

@Data
public class CommonResponse<T> implements Serializable {

    private Integer errno;
    private String errmsg;
    private T data;

    public CommonResponse(){
        this.errno= Status.SUCCESS.getCode();
        this.errmsg=Status.SUCCESS.getMessage();
    }
    public CommonResponse(Integer errno,String errmsg){
        this.errno=errno;
        this.errmsg=errmsg;
        this.data=data;
    }

    public CommonResponse(Integer errno,String errmsg,T data){
        this.errno=errno;
        this.errmsg=errmsg;
        this.data=data;
    }
    //成功返回，没有带数据
    public static CommonResponse message() {
        return new CommonResponse(Status.SUCCESS.getCode(),Status.SUCCESS.getMessage(),null);
    }

    //成功返回，带数据
    public static <T> CommonResponse<T> success(T data){
        CommonResponse<T> commonResponse=new CommonResponse<T>();
        commonResponse.setData(data);
        return commonResponse;
    }

    //失败返回，没有带数据
    public static <T> CommonResponse<T> fail(){
        return new CommonResponse(Status.ERROR.getCode(),Status.ERROR.getMessage(),null);

    }
    //失败返回，没有带数据
    public static <T> CommonResponse<T> fail(Integer errno,String errmsg){
        return new CommonResponse(errno,errmsg,null);

    }
    //失败返回，带信息
    public static <T> CommonResponse<T> fail(String message){
        return new CommonResponse(Status.ERROR.getCode(),message,null);

    }

    //传Status和data进来
    public static <T> CommonResponse<T> status(Status status,T data){
        return new CommonResponse<T>(status.getCode(),status.getMessage(),data);
    }

    //小程序端未登录，返回902
    public static <T> CommonResponse<T> unLogin(){
        return new CommonResponse(902,"请登录",null);

    }

}
